<template>
  <div class="nursing-project-container">
    <h2 class="title">护理项目</h2>

    <!-- 查询表单 -->
    <el-form :inline="true" :model="searchForm" class="search-form">
      <div class="search-left">
        <el-form-item label="名称:">
          <el-input v-model="searchForm.name" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="状态:">
          <el-select v-model="searchForm.status" placeholder="请选择状态" style="width: 150px;">
            <el-option label="全部" value="" />
            <el-option label="启用" value="1" />
            <el-option label="停用" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="init">查询</el-button>
          <el-button type="default" @click="handleReset">重置</el-button>
        </el-form-item>
      </div>
      <div class="search-right">
        <el-button type="success" @click="handleAdd">添加</el-button>
      </div>
    </el-form>

    <!-- 表格 -->
    <el-table :data="tableData" v-loading="loading" border style="width: 100%; background: #f5f7fa;"
      :header-cell-style="{ background: '#f9fafc', color: '#333', textAlign: 'center' }"
      :cell-style="{ textAlign: 'center' }">
      <el-table-column prop="name" label="名称" />
      <el-table-column prop="price" label="价格" />
      <el-table-column prop="frequency" label="执行次数" />
      <el-table-column prop="cycle" label="执行周期" />
      <el-table-column prop="description" label="描述" />
      <el-table-column prop="status" label="状态" width="100">
        <template #default="scope">
          <el-tag :type="scope.row.status === '1' ? 'success' : 'info'">
            {{ scope.row.status === '1' ? '启用' : '停用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button type="warning" size="small" @click="del(scope.row)" plain>停用</el-button>
          <el-button type="success" size="small" @click="edit(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-bar">
      <el-pagination v-model:current-page="pagination.cur" v-model:page-size="pagination.size" :page-sizes="[2, 5, 10]"
        layout="total, sizes, prev, pager, next, jumper" :total="pagination.total" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    </div>

    <!-- 弹窗组件 -->
    <EditNursingProject ref="editDialogRef" @refresh="init" />
    <AddNursingProject ref="addDialogRef" @refresh="init" />
  </div>
</template>

<script setup>
import { ref, onMounted} from 'vue'
import AddNursingProject from '@/components/nursing/nursingProject/AddNursingProject.vue'
import EditNursingProject from '@/components/nursing/nursingProject/EditNursingProject.vue'
import { ElMessageBox, ElMessage } from 'element-plus'
import {inject} from 'vue'
const axios = inject("axios")


const searchForm = ref({
  name: '',
  status: '1'
})

const tableData = ref([])
const loading = ref(false)
const pagination = ref({
  cur: 1,
  size: 5,
  total: 0
})

const init = () => {
  pagination.value.cur = 1
  query(1)
}

const query = async (page) => {
  loading.value = true
  try {
    const res = await axios.get('/nursingProject/pageNursingProject', {
      params: {
        cur: page,
        number: pagination.value.size,
        name: searchForm.value.name || '',
        status: searchForm.value.status || ''
      }
    })
    if (res.data.status === 200) {
      tableData.value = Array.isArray(res.data.data) ? res.data.data : []
      pagination.value.total = res.data.total || 0
    } else {
      tableData.value = []
      pagination.value.total = 0
      ElMessage.warning(res.data.msg || '加载失败')
    }
  } catch (err) {
    ElMessage.error('请求失败：' + err.message)
  } finally {
    loading.value = false
  }
}

const handleCurrentChange = (page) => {
  pagination.value.cur = page
  query(page)
}

const handleSizeChange = (size) => {
  pagination.value.size = size
  pagination.value.cur = 1
  query(1)
}

const handleReset = () => {
  searchForm.value.name = ''
  searchForm.value.status = '1'
  init()
}

const addDialogRef = ref()
const editDialogRef = ref()

const handleAdd = () => addDialogRef.value.open()
const edit = (row) => editDialogRef.value.open(row)

const del = async (row) => {
  try {
    await ElMessageBox.confirm(`确认删除项目「${row.name}」吗？`, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    const res = await axios.post('http://localhost:9000/nursingProject/deleteNursingProjectById', null, {
      params: { projectId: row.projectId }
    })
    if (res.data.status === 200) {
      ElMessage.success('删除成功')
      init()
    } else {
      ElMessage.error(res.data.msg || '删除失败')
    }
  } catch {
    // 用户取消
  }
}

onMounted(init)
</script>

<style scoped>
.nursing-project-container {
  padding: 20px;
}

.title {
  font-size: 27.2px;
  font-weight: bold;
  color: #333;
  margin: 0 0 20px 0;
}

.search-form {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 20px;
}

.pagination-bar {
  display: flex;
  justify-content: flex-end;
  margin-top: 2%;
  margin-bottom: 2%;
}

.search-form {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  flex-wrap: wrap;
  margin-bottom: 20px;
}

.search-left {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}

.search-right {
  display: flex;
  align-items: center;
}

</style>
